from selenium.webdriver.common.by import By
from pages.base_page import BasePage
from selenium.webdriver.support.select import Select
class AddSjPage(BasePage):
    url_ = 'http://192.168.1.49/crm3/index.php?m=business&a=add'
    shang_locator = (By.ID, 'name')
    total_locator = (By.ID, 'total_price')
    esti_locator = (By.ID, 'estimate_price')
    zt_locator = (By.ID, 'status_id')
    cusrer_locator = (By.ID, 'customer_name')
    tbody_locator = (By.ID,'datas')
    tr_locator = (By.TAG_NAME,'tr')
    td_locator =(By.TAG_NAME,'td')
    customer_locator = (By.NAME,'customer')
    span_locator = (By.XPATH,'/html/body/div[10]/div[3]/div/button[1]/span')
    input_locator = (By.CSS_SELECTOR,'#form1>table>tfoot>tr>td>input:nth-child(1)')

    def input_sj(self,name):
        """输入商机"""
        shangji = self.find_element(self.shang_locator)
        shangji.send_keys(name)

    def input_sjprice(self,total_price):
        """输入商机金额"""
        sjprice = self.find_element(self.total_locator)
        sjprice.send_keys(total_price)

    def input_estimate_price(self,estimate_price):
        """输入预计成交价"""
        esti_price = self.find_element(self.esti_locator)
        esti_price.send_keys(estimate_price)

    def select_statusa(self,status):
        """选择状态"""
        zt = self.find_element(self.zt_locator)
        select = Select(zt)
        select.select_by_visible_text(status)


    def select_customer(self,customer_name):
        """选择商机"""
        self.find_element(self.cusrer_locator).click()
        tbod_element = self.find_element(self.tbody_locator)
        trs = tbod_element.find_elements(*self.tr_locator)
        for tr in trs:
            tds = tr.find_elements(*self.td_locator)
            if tds[1].text == customer_name:
                tds[0].find_element(*self.customer_locator).click()
                self.find_element(self.span_locator).click()
                break

    def submit(self):
        self.find_element(self.input_locator).click()

    def get_current_url(self):
        return self.driver.current_url





